<?php

include("class.phpmailer.php");
include("class.smtp.php");

class TestEmail
{
    
	public $ccMail = '';
	public $myFile = 'http://freecarbids.com/email_templates/';
	
	public function sendUpdateNotification($type, $email, $vid, $company, $imagurl)
	{
		$result = self::setUpdateNotification('sellerUpdateNotification.html', $vid, $company, $email,$imagurl);
		return $result;
	}
	private function setUpdateNotification($page, $vid, $company, $to,$imagurl)
    {
		$add_sub = '';
		if($vid != ''){
			$vehicle_result = mysql_query('SELECT year, make, model, city, state FROM vehicle WHERE vehicle_id ='.$vid) or die("Query ERROR");
			$result_value = mysql_fetch_object($vehicle_result);
			$add_sub = "Reply-[".$result_value->year."] ".$result_value->make." - ".$result_value->model. " | ".$result_value->city."/".$result_value->state;
		}
		$myfile = $this->myFile.$page;
        $content = file_get_contents($myfile);
        $body_content = self::getdata(stripslashes($content), "body:", ":body");
        //$body_content = str_replace("#url", $url, $body_content);
        $body_content = str_replace("#company", $company, $body_content);
        $body_content = str_replace("#vid", $vid, $body_content);
		$body_content = str_replace("#content", $imagurl, $body_content);
        $sub = self::getdata(stripslashes($content), "sub:", ":sub");
		$totalSub = $add_sub." : ".$sub ;
        self::doMail($totalSub, $body_content, $to, $vid);
	}
	public function sendNotificationEmail($url, $type, $email, $vid, $company, $detailHtml, $subcontent)
	{
		$result = self::setNewSubmissionNotification('sellerSubmitNotification.html', $url, $vid, $company, $email,$detailHtml, $subcontent);
		return $result;
	}
	
	private function setNewSubmissionNotification($page, $url, $vid, $company, $to, $html, $subcontent)
    {
        $myfile = $this->myFile.$page;
        $content = file_get_contents($myfile);
        $body_content = self::getdata(stripslashes($content), "body:", ":body");
        $body_content = str_replace("#url", $url, $body_content);
        $body_content = str_replace("#company", $company, $body_content);
        $body_content = str_replace("#vid", $vid, $body_content);
		$body_content = str_replace("#vdetail", $html, $body_content);
        $sub = self::getdata(stripslashes($content), "sub:", ":sub");
		$totalSub = $subcontent." : ".$sub ;
        self::doMail($totalSub, $body_content, $to, $vid);
    }

	public function sendEmail($url, $type, $to, $vid, $fname)
    	{
        $mailType = $type;
       
        if($mailType == "SellerSubmit")
        {
            $result = self::setData('sellerSubmit.html', $url, $vid, $fname, $to);
            return $result;
        }
        else if($mailType == "BidComment")
        {
            $result = self::setData('buyerBidComment.html', $url, $vid, $fname, $to);
            return $result;
        }
        else if($mailType == "SellerBidComment")
        {
            $result = self::setData('sellerBidComment.html', $url, $vid, $fname, $to);
            return $result;
        }
        else if($mailType == "BuyerSubmit")
        {
            $result = self::setData('buyerSubmit.html', $url, $vid, $fname, $to);
            return $result;
        }
        else if($mailType == "ForumReply")
        {
		$this->ccMail = 'gopinath@synergyquad.com';

            $result = self::setData('forumreply.html', $url, $vid, $fname, $to);
            return $result;
        }
else if($mailType == "AddForum")
        {
//$this->ccMail = 'gopinath@synergyquad.com';

            $result = self::setData('forumreply.html', $url, $vid, $fname, $to);
            return $result;
        }
   		else if($mailType == "ShareVehicle")
        {
            $result = self::setData('shareVehicle.html', $url, $vid, $fname, $to);
            return $result;
        }
	else if($mailType == "PartnerComment")
        {
            $result = self::setData('partnerComment.html', $url, $vid, $fname, $to);
            return $result;
        }
	else if($mailType == "mSellerSubmit")
        {
            $result = self::setData('sellerSubmit.html', $url, $vid, $fname, $to);
            return $result;
        }

    }
   
    private function setData($page, $url, $vid, $fname, $to)
    {
        $myFile = 'http://173.1.150.90/email_templates/'.$page;
        $content = file_get_contents($myFile);
        $body_content = self::getdata(stripslashes($content), "body:", ":body");
        $body_2 = str_replace("#url", $url, $body_content);
        $body_1 = str_replace("#fname", $fname, $body_2);
        $body = str_replace("#vid", $vid, $body_1);
        $sub = self::getdata(stripslashes($content), "sub:", ":sub");

        self::doMail($sub, $body, $to, $vid);
    }

    private function getdata($input, $start, $end)
    {
        $substr = substr($input, strlen($start)+strpos($input, $start), (strlen($input) - strpos($input, $end))*(-1));
        return $substr;
    }
   
    private function doMail($sub, $body, $toMail, $vid)
    {
        $mail             = new PHPMailer();
        $mail->IsSMTP();
        $mail->SMTPAuth   = true;                  // enable SMTP authentication
        $mail->Host       = "smtp.autogator.com";      // sets GMAIL as the SMTP server
        $mail->Port       = 25;                 // set the SMTP port
        $mail->Username   = "noreply@autogator.com";  //GMAIL username
        $mail->Password   = "lmgrad1";            // GMAIL password
        $mail->From       = "noreply@autogator.com";
        $mail->FromName   = "AutoGator Buys Cars";
        $mail->Subject    = "Bid# ".$vid.", ".$sub;
        $mail->AltBody    = "This is the body when user views in plain text format"; //Text Body
        $mail->WordWrap   = 50; // set word wrap
        $mail->MsgHTML($body);
        $mail->AddReplyTo("noreply@autogator.com","AutoGator Buys Cars");

        $mail->AddAddress($toMail,"Autogator");
if($this->ccMail != '')
{
	$mail->AddCC($this->ccMail, 'Gopi') ;
}
        
        $mail->IsHTML(true); // send as HTML
       
        if(!$mail->Send())
        {
          return  "Mailer Error: " . $mail->ErrorInfo;
        }
        else
        {
          return "Message has been sent";
        }
    }
}
?>